package com.uroad.jilinpda.util;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.uroad.lib.util.encrypt.SecurityUtil;

import static android.content.Context.TELEPHONY_SERVICE;

/**
 * Created by hrd on 2017/12/21 0021.
 */

public class ImeiUtil {

    public static String getMyImei(Context context) {
        String result = "0";
        String szImei = getImei(context);

        String m_szDevIDShort = getPUID();

        String m_szAndroidID = getAndroidID(context);

        String m_szWLANMAC = getWLANMAC(context);

        String m_szLongID = szImei + m_szDevIDShort
                + m_szAndroidID + m_szWLANMAC;

        try {
            if (TextUtils.isEmpty(m_szLongID)) {
                m_szLongID = "0";
                return m_szLongID;
            }
            result = SecurityUtil.EncoderByMd5(m_szLongID);
        } catch (Exception e) {
            e.printStackTrace();
            return "0";
        }
        return result;
    }

    public static String getImei(Context context) {
        try {
            String imei;
            TelephonyManager TelephonyMgr = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                //没有权限
                imei = "";
            } else {
                imei = TelephonyMgr.getDeviceId();
            }
            if (TextUtils.isEmpty(imei)) {
                imei = "";
            }
            return imei;
        } catch (Exception e) {
            return "";
        }

    }

    public static String getPUID() {
        try {
            String m_szDevIDShort = "35" + //we make this look like a valid IMEI
                    Build.BOARD +
                    Build.BRAND +
                    Build.CPU_ABI +
                    Build.DEVICE +
                    Build.DISPLAY +
                    Build.HOST +
                    Build.ID +
                    Build.MANUFACTURER +
                    Build.MODEL +
                    Build.PRODUCT +
                    Build.TAGS +
                    Build.TYPE +
                    Build.USER; //13 digits
            if (TextUtils.isEmpty(m_szDevIDShort)) {
                m_szDevIDShort = "";
            }
            return m_szDevIDShort;
        } catch (Exception e) {
            return "";
        }

    }

    public static String getAndroidID(Context context) {
        try {
            String m_szAndroidID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            if (TextUtils.isEmpty(m_szAndroidID)) {
                m_szAndroidID = "";
            }
            return m_szAndroidID;
        } catch (Exception e) {
            return "";
        }
    }

    public static String getWLANMAC(Context context) {
        try {
            WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            String m_szWLANMAC = wm.getConnectionInfo().getMacAddress();
            if (TextUtils.isEmpty(m_szWLANMAC)) {
                m_szWLANMAC = "";
            }
            return m_szWLANMAC;
        } catch (Exception e) {
            return "";
        }
    }
}
